package com.atguigu.spring.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.TypeUtils;

import java.util.Arrays;

@Component
@Aspect
public class AuthAspect {
    @Pointcut("args(int, int)")
    public void pointCut() {}

    @Before("pointCut()")
    public void authStart(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        String methodName = methodSignature.getMethod().getName();
        String paramTypes = Arrays.toString(methodSignature.getParameterTypes());
        String params = Arrays.toString(joinPoint.getArgs());
        // 4种 Spring 提供的常用工具类
//        ClassUtils.getClassFileName();
//        ClassUtils.getShortName();
//        TypeUtils.isAssignable();
//        ReflectionUtils.findMethod();
//        AnnotationUtils.findAnnotation();
        System.out.println("【切面 - 权限】：" + methodName + paramTypes + "开始, 参数：" + params);
    }
    @After("pointCut()")
    public void logEnd(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        System.out.println("【切面 - 权限】：" + methodSignature + "结束");
    }

    @AfterReturning(value = "pointCut()",
            returning = "result")
    public void logResult(JoinPoint joinPoint, Object result) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        System.out.println("【切面 - 权限】：" + methodSignature + "结果：" + result);
    }

    @AfterThrowing(value = "pointCut()",
            throwing = "e")
    public void logException(JoinPoint joinPoint, Throwable e) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        System.out.println("【切面 - 权限】：" + methodSignature + "异常:"+e.getMessage());
    }
}
